package com.example.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.example.entity.MCategory;
import com.example.entity.MPost;
import com.example.service.IMCategoryService;

import com.example.service.IMPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.util.List;

@Component
public class ContextStartUp implements ApplicationRunner, ServletContextAware {
    @Autowired
    IMCategoryService categoryService;
    ServletContext servletContext;

    @Autowired
    IMPostService postService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        QueryWrapper<MCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<MCategory> categories = categoryService.list(queryWrapper);
        servletContext.setAttribute("categorys",categories);
        postService.initWeekRank();
    }


    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
